package com.jxn.ctrip.spider;

import com.alibaba.fastjson.JSONObject;
import com.jxn.ctrip.entity.Hotel;
import com.jxn.ctrip.util.DebugUtil;
import com.jxn.ctrip.util.OkHttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jxn.ctrip.util.HttpUtil;

import java.util.HashMap;
import java.util.List;

/**
 * 抓取酒店详细数据
 *
 * @author jnq
 */
public class HotelDetailSpider {

    public static void main(String[] args) {
        HotelSpider hotelSpider = new HotelSpider();
        List<Hotel> dbHotels = hotelSpider.getDBHotels();

        HotelDetailSpider spider = new HotelDetailSpider();
//		spider.getHotelDetail("436290");
//        spider.getHotelRoomList("441351");

        int count = 0;
        for (Hotel hotel : dbHotels) {
            System.out.println(hotel.getName());
            spider.getHotelRoomList(hotel.getId());
            System.out.println("\n");

            if (DebugUtil.isDEBUG() && count++ > 2) {
                break;
            }
        }
    }

    String hotelRoomUrl = "http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx";

    void getHotelRoomList(String hotelId) {
//		http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?psid=&MasterHotelID=436290&hotel=436290
        System.out.println("------ " + hotelId + "  -----");

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("MasterHotelID", hotelId);
        params.put("hotel", hotelId);

//		String result = HttpUtil.getInstance().httpGetRoom(params, hotelId, hotelRoomUrl);
        String result = OkHttpUtil.getRoom(hotelRoomUrl, params, hotelId);

        // 解析酒店数据
        JSONObject resultObj = JSONObject.parseObject(result);
        String html = resultObj.getString("html");

        decodeRoomHtml(html);
    }

    void decodeRoomHtml(String roomHtml) {
        Document root_document = Jsoup.parse(roomHtml);
        Elements trs = root_document.getElementsByTag("tr");

        int ix = 1;
        for (Element atr : trs) {
            Elements tds = atr.getElementsByTag("td");
            if (tds.size() > 0) {
                StringBuilder sbd = new StringBuilder();

                if (tds.size() > 5) {
                    sbd.append(ix++ + " :");
                }
                for (Element atd : tds) {
                    sbd.append(atd.text()).append("  |  ");
                }

                System.out.println(sbd.toString());
            }
        }
        System.out.println("");
    }


    void getHotelDetail(String hotelId) {
        long starttime = System.currentTimeMillis();
        StringBuffer htmlUrl = new StringBuffer();
        htmlUrl.append("http://hotels.ctrip.com/hotel/").append(hotelId).append(".html");

        String result = HttpUtil.getInstance().httpGet(null, htmlUrl.toString());

        Document document = Jsoup.parse(result);
        Elements elements = document.select("div.list_wide_mod2");

        System.out.println(elements.size());
        System.out.println(System.currentTimeMillis() - starttime);
    }


}
